﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartHotel.Dto;
using SmartHotel.Tag.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.Tag
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppTagService : ApplicationService, ISmartHotelAppTagService
    {
        private readonly IRepository<SmartHotel_TagTable, int> _repository;

        public SmartHotelAppTagService(IRepository<SmartHotel_TagTable,int> repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteTag(int id)
        {
            await _repository.DeleteAsync(id);
            return new ResultDto<bool> { Code=Result.Success,Message="删除成功"};
        }
        /// <summary>
        /// 展示标签
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<TagListDto>>> GetTagList()
        {
            var list = await _repository.GetListAsync();
            var dto = ObjectMapper.Map<List<SmartHotel_TagTable>, List<TagListDto>>(list);
            return new ResultDto<List<TagListDto>> { Code=Result.Success,Data= dto };
        }
        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="createTagDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateTagDto>> InsertTag(CreateTagDto createTagDto)
        {
            var taglist = await _repository.GetQueryableAsync();
            var tagcount = taglist.Where(x => x.IsDeleted == false).Count();
            if (tagcount >= 4)
            {
                return new ResultDto<CreateTagDto> { Code = Result.Failed, Message = "标签不能多于四条" };
            }
            else
            {
                var list = await _repository.InsertAsync(ObjectMapper.Map<CreateTagDto, SmartHotel_TagTable>(createTagDto));
                return new ResultDto<CreateTagDto> { Code = Result.Success, Data = list, Message = "添加成功" };
            }
            
        }
    }
}
